package org.jsola.hr.web;

import lombok.Data;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CrmCustomerImportRecordAddDTO;
import org.jsola.hr.dto.CrmCustomerImportRecordUpdateDTO;
import org.jsola.hr.query.CrmCustomerImportRecordQuery;
import org.jsola.hr.service.ICrmCustomerImportRecordService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.validation.Valid;


/**
 * 导入客户记录表
 *
 * @author zhr
 */
@RestController("hrCrmCustomerImportRecordController")
@RequestMapping(HrConstants.API_PATH + "/v1/crmCustomerImportRecords")
public class CrmCustomerImportRecordController implements ICrmCustomerImportRecordControllerAPI {

    @Autowired
    private ICrmCustomerImportRecordService crmCustomerImportRecordService;

    @Override
    @GetMapping("/getErrorCustomerData/{crmCustomerImportRecordId}")
    public ResponseEntity<Resource> getErrorCustomerData(@PathVariable String crmCustomerImportRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // 模板文件
        InputStream is = crmCustomerImportRecordService.getErrorCustomerData(crmCustomerImportRecordId, tokenUser);
        ParamException.notNull(is, "模板生成失败");

        // 编码文件名，防止乱码
        String filenames = new String("ErrorData".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE,"application/vnd.ms-excel")
                .body(new InputStreamResource(is));
    }

    @Override
    @PostMapping("/importCustomerInfo")
    public Result<Integer> importCustomerInfo(CrmCustomerImportRecordQuery crmCustomerImportRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmCustomerImportRecordService.importCustomerInfo(crmCustomerImportRecordQuery, tokenUser)
        );
    }

    @Override
    @PostMapping(value = "/importFile",headers = "content-type=multipart/form-data")
    public Result<CrmCustomerImportRecordVO> importFile(MultipartFile file) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                crmCustomerImportRecordService.importFile(file, tokenUser)
        );
    }

    @Override
    @GetMapping("/downloadCustomerTemplate")
    public ResponseEntity<Resource> downloadCustomerTemplate() {
        // 模板文件
        InputStream is = getClass().getClassLoader().getResourceAsStream("excel/customerInfo.xlsx");
        ParamException.notNull(is, "customerInfo.xlsx模板没有找到");
        // 编码文件名，防止乱码
        String filenames = new String("潜在客户信息导入表.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE,"application/vnd.ms-excel")
                .body(new InputStreamResource(is));
    }

    @Override
    @PostMapping
    public Result<CrmCustomerImportRecordVO> save(@Valid @RequestBody CrmCustomerImportRecordAddDTO crmCustomerImportRecordAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增导入客户记录表权限校验

        return Result.success(
                crmCustomerImportRecordService.save(crmCustomerImportRecordAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CrmCustomerImportRecordUpdateDTO crmCustomerImportRecordUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改导入客户记录表权限校验

        int count = crmCustomerImportRecordService.update(crmCustomerImportRecordUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{crmCustomerImportRecordId}")
    public Result<Integer> delete(@PathVariable String crmCustomerImportRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除导入客户记录表权限校验

        int count = crmCustomerImportRecordService.deleteByIds(tokenUser, crmCustomerImportRecordId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] crmCustomerImportRecordIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除导入客户记录表权限校验

        int count = crmCustomerImportRecordService.deleteByIds(tokenUser, crmCustomerImportRecordIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{crmCustomerImportRecordId}")
    public Result<CrmCustomerImportRecordVO> findById(@PathVariable String crmCustomerImportRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看导入客户记录表权限校验

        return Result.success(
            crmCustomerImportRecordService.selectById(crmCustomerImportRecordId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CrmCustomerImportRecordListVO>> listAll(@Valid CrmCustomerImportRecordQuery crmCustomerImportRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看导入客户记录表权限校验

        return Result.success(
            crmCustomerImportRecordService.select(crmCustomerImportRecordQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CrmCustomerImportRecordListVO>> page(@Valid CrmCustomerImportRecordQuery crmCustomerImportRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            crmCustomerImportRecordService.selectPage(crmCustomerImportRecordQuery, tokenUser.getSiteId())
        );
    }


}

